package com.neborosoft.FlyingDog.android.playing.modes;

import android.app.Activity;
import android.os.Parcelable;
import android.widget.AbsListView;
import com.neborosoft.FlyingDog.android.gui.adapters.listview.SongsPlayListAdapter;
import com.neborosoft.FlyingDog.logic.database.AudioDataManager;
import com.neborosoft.FlyingDog.logic.database.content.identified.Audio;
import com.neborosoft.FlyingDog.logic.search.suggestions.AudioSuggestionsProvider;
import com.neborosoft.FlyingDog.logic.search.suggestions.SuggestionsProvider;
import com.neborosoft.android.gui.wrappers.ListViewSwitcher;
import com.neborosoft.lib.collections.list.NavigationList;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 01.12.12
 * Time: 18:33
 * To change this template use File | Settings | File Templates.
 */
public class AllSongsPlayingMode extends PlayingModeWithSuggestions{
    private SongsPlayListAdapter songsPlayListAdapter;
    private AudioDataManager audioDataBase = AudioDataManager.getInstance();

    private List<Audio> lastAdapterElements;

    public AllSongsPlayingMode(Activity activity, ListViewSwitcher listViewSwitcher) {
        super(activity, listViewSwitcher);

        AbsListView listView = (AbsListView)activity.findViewById(Constants.listViewSwitcherResource);
        songsPlayListAdapter = new SongsPlayListAdapter(activity.getApplicationContext(), listView);
    }

    @Override
    public Object performSearch(String query) {
        return AudioDataManager.getInstance().getSongs(query);
    }

    void updateAdapterAndElements(List<Audio> elements){
        SongsPlayListAdapter selectedSongsAdapter = SongsPlayListAdapter.getSelectedSongsAdapter();
        boolean selectedSongsPlayListAdapterContent =
                SongsPlayListAdapter.selectedSongsAdapterHasSpecifiedContent(elements);
        if(selectedSongsPlayListAdapterContent){
            songsPlayListAdapter = selectedSongsAdapter;
            getListViewSwitcher().setAdapter(selectedSongsAdapter);
            return;
        }

        if(songsPlayListAdapter == selectedSongsAdapter && !selectedSongsPlayListAdapterContent){
            songsPlayListAdapter = new SongsPlayListAdapter(getActivity().getApplicationContext(),
                    getListViewSwitcher().getListView());
            getListViewSwitcher().setAdapter(songsPlayListAdapter);
        }

        songsPlayListAdapter.setElements(elements);
        NavigationList.resumePageLoadingOfNavigationList(elements);
    }

    @Override
    public void onSearchFinished(Object searchResult) {
        NavigationList<Audio> songs = (NavigationList<Audio>)searchResult;
        songs.setOnNextPageLoadingFinished(new NavigationList.OnNextPageLoadingFinished() {
            @Override
            public void onLoadingFinished() {
                songsPlayListAdapter.notifyDataSetChanged();
            }
        });

        updateAdapterAndElements(songs);

        ListViewSwitcher listViewSwitcher = getListViewSwitcher();
        listViewSwitcher.setAdapter(songsPlayListAdapter);
    }

    @Override
    public void onAnotherModeSelected() {
        lastAdapterElements = songsPlayListAdapter.getElements();
        NavigationList.pausePageLoadingOfNavigationList(lastAdapterElements);
    }

    private void setListView(){
        getListViewSwitcher().switchTo(Constants.listViewSwitcherResource, songsPlayListAdapter);
    }

    @Override
    public void showInListView(boolean restoreState) {
        List<Audio> audios;
        ListViewSwitcher listViewSwitcher = getListViewSwitcher();

        if(!restoreState){
            audios = audioDataBase.getSongs();
        } else {
            audios = lastAdapterElements;
        }

        setListView();
        updateAdapterAndElements(audios);

        if(!restoreState){
            listViewSwitcher.getListView().setSelection(0);
        }
    }

    @Override
    protected SuggestionsProvider createSuggestionsProvider() {
        return new AudioSuggestionsProvider();
    }

    @Override
    protected SongsPlayListAdapter getSongsPlayListAdapter() {
        return songsPlayListAdapter;
    }

    public void showSelectedPlaylist(){
        SongsPlayListAdapter selectedSongsPlayListAdapter = SongsPlayListAdapter.getSelectedSongsAdapter();
        if(selectedSongsPlayListAdapter == null){
            return;
        }

        setListView();
        ListViewSwitcher listViewSwitcher = getListViewSwitcher();

        if(songsPlayListAdapter != selectedSongsPlayListAdapter){
            songsPlayListAdapter = selectedSongsPlayListAdapter;
        }

        listViewSwitcher.setAdapter(songsPlayListAdapter);

        songsPlayListAdapter.scrollListViewToSelectedIndex();
    }

    private static class State{
        Parcelable listViewState;
        List adapterElements;
    }

    @Override
    public Object getState() {
        State state = new State();

        state.adapterElements = songsPlayListAdapter.getElements();
        NavigationList.pausePageLoadingOfNavigationList(state.adapterElements);

        state.listViewState = getListViewState();
        return state;
    }

    @Override
    public void restoreState(Object stateObject) {
        State state = (State)stateObject;
        setListView();
        updateAdapterAndElements(state.adapterElements);
        getListView().onRestoreInstanceState(state.listViewState);
    }
}
